﻿#include "Bullet.h"
//#include "Global.h"
#include "Resource.h"

Bullet::Bullet(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 BackBuffer, LPD3DXSPRITE SpriteHandler, float x, float y, bool isLeft) : Object(x, y)
{
	//this->SetID(ID);
	p_sprite = new Sprite(SpriteHandler, ResourceMng::GetInst(d3ddv, BackBuffer)->GetTexture(L"..\\Resources\\Images\\bullet.png"), 125, 0);

	p_sprite->setFrame(0,4);	//frame an

	_xOld = x;
	_yOld = y;

	width = p_sprite->getTexture()->getWidth();
	height = p_sprite->getTexture()->getHeight();

	if (isLeft == true)
	{
		veloc_x = - 0.9;
	} 
	else
	{
		veloc_x =  0.9;
	}

	veloc_y = 0;
	accel_y = 0;

	isGravity = true;

	//container = NULL;


	obj_state = IS_MOVING;

	timeExplosion = 0;
	timeLive = 0;
}

Bullet::~Bullet()
{
	if (p_sprite != NULL)
	{
		delete p_sprite;
	}
}

void Bullet::render(int cameraX, int cameraY)
{
	if (obj_state != IS_REMOVE)
	{
		p_sprite->render(pos_x, pos_y, cameraX, cameraY);
	}

}

OBJECT_TYPE Bullet::getObjectType()
{

	return OBJECT_ITEM;
}

//void Bullet::ResetRect()
//{
//	_rectBond.left = _x;
//	_rectBond.right = _rectBond.left + _sprite->texture->_Width;
//	_rectBond.top = _y;
//	_rectBond.bottom = _rectBond.top + _sprite->texture->_Height;
//}

void Bullet::update(list<Object*>* obj_list, DxInput* input, float gameTime)
{
	switch(obj_state)
	{
	case IS_IDLE:
		//CheckObjects(staObjs,dynObjs);
		break;

	case IS_ACTIVING:
		pos_x += veloc_x * gameTime;
		pos_y += veloc_y*gameTime + 1/2 * accel_y * gameTime * gameTime;
		if(pos_y < this->_yOld - 32-5)
			changeState(IS_MOVING);
		break;

	case IS_MOVING:
		move(obj_list, gameTime);//Move(gameTime,staObjs,dynObjs);
		timeLive += gameTime;
		if (timeLive > 4000)
		{
			timeLive = 0;
			changeState(IS_REMOVE);
		}
		break;

	case IS_EXPLOSION:
		timeExplosion += gameTime;
		if (timeExplosion > 200)
		{
			timeExplosion = 0;
			changeState(IS_REMOVE);
		}

		break;
	}

	p_sprite->update(gameTime);
	//ResetRect();
}



void Bullet::move(list<Object*>* obj_list, float TPF)
{
	Object* obj;
	list<Object*>::iterator i;

	//Theo phương x
	pos_x += veloc_x*TPF;	


	//for(i=staObjs->begin();i!=staObjs->end();i++)
	//{
	//	obj=*i;
	//	//if(obj->GetID()>=20&&obj->GetID()<=29) //Nếu là thùng, đá , gạch...
	//	if (obj->GetTypeObject() == OBJECT_LAND)
	//		if(Collision::RectVsRect(this->GetBond() , obj->GetBond()) == true)	
	//		{
	//			//_vx=-_vx;
	//			_vx = 0;
	//			_x = _xOld;
	//			changeState(IS_EXPLOSION);
	//		}
	//}



	//Theo phương y
	accel_y = GRAVITY;
	veloc_y -= accel_y * TPF;
	pos_y += veloc_y * TPF + 1.0/2 * accel_y * TPF * TPF;



	/*for(i=staObjs->begin();i!=staObjs->end();i++)
	{
	obj=*i;

	if (obj->GetTypeObject() ==OBJECT_LAND)
	if(Collision::RectVsRect(this->GetBond() , obj->GetBond())==true)	
	{
	_accelY=0;
	_vy=-0.8;
	_y=_yOld;
	}
	}*/

	//for(i=staObjs->begin();i!=staObjs->end();i++)
	//{
	//	obj=*i;
	//	if (obj->GetID() == 42)			//pirhana
	//		if(Collision::RectVsRect(this->GetBond() , obj->GetBond())==true)	
	//		{
	//			if (object_State != IS_REMOVE || object_State != IS_IDLE)
	//			{
	//				obj->ChangeState(IS_REMOVE);
	//				this->changeState(IS_EXPLOSION);
	//			}
	//		}
	//}
	/*for(i=dynObjs->begin();i!=dynObjs->end();i++)
	{
	obj=*i;
	if(Collision::RectVsRect(this->GetBond() , obj->GetBond())==true)	
	{
	switch(obj->GetID())
	{
	case 11:
	if (object_State == ES_ACTIVING)
	{
	obj->ChangeState(ES_FALL);
	this->changeState(IS_EXPLOSION);
	}
	break;
	case 12:
	if (object_State == ES_ACTIVING || object_State == ES_STOP)
	{
	obj->ChangeState(ES_FALLBULLET);
	this->changeState(IS_EXPLOSION);
	}
	break;
	}
	}
	}*/

	for (i = obj_list->begin(); i != obj_list->end(); i++)
	{
		obj = *i;
		//if (obj->getObjectType() == OBJECT_LAND)
		//{

		Box box(this->_xOld,
			this->_yOld - this->height, 
			width, 
			height, 
			veloc_x * TPF, 
			veloc_y * TPF + 1.0f/2.0f * accel_y * TPF * TPF);


		Box block(obj->getPosX(),
			obj->getPosY() - obj->getHeight(),// - 0.11,
			obj->getWidth(),
			obj->getHeight()
			);

		Box boxphase = Collision::GetSweptBroadphaseBox(box);
		if (Collision::AABBCheck(boxphase, block))
		{
			float normalx = 0;
			float normaly = 0;
			float collision_time = Collision::SweptAABB(box, block, normalx, normaly);
			if (0 < collision_time && collision_time < 1)
			{

				switch(obj->getIDObjType())
				{
				case 11:	//goomba
					if (obj->getObjState() == ES_ACTIVING)
					{
						obj->changeState(ES_FALL);
						this->changeState(IS_EXPLOSION);
					}
					break;

				case 12:	//koopba
					if (obj->getObjState() == ES_ACTIVING || obj->getObjState() == ES_STOP)
					{
						obj->changeState(ES_FALLBULLET);
						this->changeState(IS_EXPLOSION);
					}
					break;
				case 42:	//Pirhana, cay an thit
					if (obj->getObjState() != IS_REMOVE || obj->getObjState() != IS_IDLE)
					{
						obj->changeState(IS_REMOVE);
						this->changeState(IS_EXPLOSION);
					}
					break;
				}

				if (normalx == 1 && normaly == 0)		//va cham tu phai -> trai
				{
					pos_x = _xOld + veloc_x * collision_time;// + 1.0f/2.0f * accel_x * collision_time *collision_time;
					accel_x = 0;
					veloc_x = 0;//-veloc_x;

					changeState(IS_EXPLOSION); 
				} 
				else if (normalx == -1 && normaly == 0)		//va cham tu trai -> phai
				{
					pos_x = _xOld + veloc_x * collision_time;// + 1.0f/2.0f * accel_x * collision_time *collision_time;
					accel_x = 0;
					veloc_x = 0;//-veloc_x;

					changeState(IS_EXPLOSION);
				}
				else if (normalx == 0 && normaly == -1)		//va cham tu duoi -> tren
				{
					pos_y = _yOld + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time;
					veloc_y = -veloc_y;
					accel_y = 0;
				}
				else if (normalx == 0 && normaly == 1)		//va cham tu tren -> duoi
				{


					pos_y = _yOld + veloc_y * collision_time + 1.0f/2.0f * accel_y * collision_time *collision_time + 0.5;
					accel_y = 0;
					veloc_y = 0.8;


				}

				/*if (this->_yOld - this->height <= obj->getPosY() + 1.5)
				{
				isGravity = false;
				}
				else
				{
				isGravity = true;
				}*/





			}

			//}
			//else
			//{
			//	/*if (this->_yOld - this->height > obj->getPosY() + 1.5)
			//	{
			//		isGravity = true;
			//	}*/
			//	 
			//}



		}
	}


	_xOld = pos_x;
	_yOld = pos_y;
}

void Bullet::changeState(int state)
{
	obj_state = state;
	switch(state)
	{
	case IS_ACTIVING:
		veloc_y = 4.00;
		break;

	case IS_MOVING:
		veloc_y = 0;
		veloc_x = 0.100;
		break;

	case IS_EXPLOSION:
		p_sprite->setFrame(5,8);
		break;

	case IS_REMOVE:
		break;
	}
}